package com.hdjk.view.util;

import java.util.Comparator;

import android.text.TextUtils;

import com.lst.projectlib.entity.BaseEntity;

public class ComparatorCharItem implements Comparator<Object> {

	@Override
	public int compare(Object object1, Object object2) {
		int flag = 0;
		try {
			BaseEntity item1 = (BaseEntity) object1;
			BaseEntity item2 = (BaseEntity) object2;

			if (item1.getAbbr() != null && !item1.getAbbr().equals("")
					&& item2.getAbbr() != null && !item2.getAbbr().equals("")) {

				String firstChar1 = item1.getAbbr().substring(0, 1);
				String firstChar2 = item2.getAbbr().substring(0, 1);

				if (firstChar1.equalsIgnoreCase("#")
						&& !firstChar2.equalsIgnoreCase("#")) {
					flag = 1;
				} else if (!firstChar1.equalsIgnoreCase("#")
						&& firstChar2.equalsIgnoreCase("#")) {
					flag = -1;
				} else if (firstChar1.equalsIgnoreCase("#")
						&& firstChar2.equalsIgnoreCase("#")) {
					flag = 0;
				} else {
					String firstAbbr1 = item1.getFirstAbbr();
					String firstAbbr2 = item2.getFirstAbbr();
					if (!TextUtils.isEmpty(firstAbbr1)
							&& !TextUtils.isEmpty(firstAbbr2)) {
						flag = firstAbbr1.compareToIgnoreCase(firstAbbr2);
						if (flag == 0) {
							String name1 = item1.getName();
							String name2 = item2.getName();

							if (!TextUtils.isEmpty(name1)
									&& !TextUtils.isEmpty(name2)) {
								flag = name1.substring(0, 1)
										.compareToIgnoreCase(
												name2.substring(0, 1));
								if (flag == 0) {
									flag = item1.getAbbr().compareToIgnoreCase(
											item2.getAbbr());
								}
							}
						}
					} else {
						flag = item1.getAbbr().compareToIgnoreCase(
								item2.getAbbr());
					}
				}
			}

		} catch (Exception e) {

		}

		return flag;
	}
}
